//
//  ShopTableViewCell.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "ShopTableViewCell.h"
#import "HTTPTool.h"
#import "AFNetworking.h"
#import "shopModel.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "shopDetaiksViewController.h"
@implementation ShopTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.shopScrollView =[[UIScrollView alloc]initWithFrame:CGRectZero];
self.shopScrollView.backgroundColor=[UIColor whiteColor];
       [self.contentView addSubview:self.shopScrollView];
      
    self.shopScrollView.pagingEnabled=NO;
    //是否边界 回弹
    self.shopScrollView.bounces=NO;
    //是否有水平滚动条
    self.shopScrollView.showsHorizontalScrollIndicator=NO;
    self.buttonarr =[NSMutableArray array];
//    [self createmyimage];
    
//    [self.contentView addSubview:self.myImageView];

    
    

    [_shopArr release];
    [_shopScrollView release];
      [self createData];
    
    return  self;
    
    
}
- (void)createData
{
    self.shopArr=[NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/v1/api/app/merchants/fine.json?city=0&page=1&per_page=10" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSMutableArray  *arr = [NSMutableArray array];
        arr = dic[@"merchants"];
      
        for (NSDictionary *tempdic in arr) {
            shopModel *shop = [[shopModel alloc]init];
            [shop setValuesForKeysWithDictionary:tempdic];
            [self.shopArr addObject:shop];
         
            [shop release];
        }
        [self createscrollview];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

-(void)createscrollview{
    self.shopScrollView.contentSize=CGSizeMake(115*(self.shopArr.count), 0);
    for (NSInteger i=0; i<self.shopArr.count; i++) {
        shopModel  *shop =[[shopModel alloc]init];
        shop = self.shopArr[i];
        self.viewd=[[UIView alloc]initWithFrame:CGRectMake((i)*115, 0, 110, 100)];
        self.viewd.backgroundColor=[UIColor whiteColor];
     
        self.mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.mybutton.frame = CGRectMake(5, 5, 105, 70);
        [self.buttonarr addObject: self.mybutton];

        [self.shopScrollView addSubview:self.viewd];
        self.mybutton.layer.masksToBounds=YES;
        self.mybutton.layer.cornerRadius=10;
        self.mybutton.layer.borderWidth=0.4;
        self.myLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.myLabel.frame=CGRectMake(5, 72, 110, 30);
        self.myLabel.layer.borderWidth=0.1;
    
        self.myLabel.font =[UIFont systemFontOfSize:12];
        self.myLabel.text=shop.name;
        self.myLabel.numberOfLines=0;
          [self.mybutton sd_setImageWithURL:[NSURL URLWithString:shop.logo_path] forState:UIControlStateNormal];
        [self.viewd addSubview:self.mybutton];
        [self.mybutton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewd addSubview:self.myLabel];
     
        [_mybutton release];
        [_myLabel release];
        [_viewd release];     }

}
-(void)click:(UIButton *)button{
       for (NSInteger i=0; i<self.buttonarr.count; i++) {
           if (button == self.buttonarr[i]) {
               shopModel   *shop =[[shopModel alloc]init];
               shop = self.shopArr[i];
               [self.delegatey click:button model:shop];
            
           }
       }
 
}
-(void)layoutSubviews
{
    self.shopScrollView.frame=CGRectMake(0, 0, self.contentView.frame.size.width, 100);    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)awakeFromNib {
    // Initialization code
}

@end
